#ifndef _MODULE_HITLIST
#define _MODULE_HITLIST
#include "page.module.js"
#include "prefs.module.js"
#include "boss.module.js"
#include "console.module.js"
#include "task.lib.js"
#include "ajax.lib.js"
#include "cache.lib.js"
#include "utility.lib.js"
#include "usolib.lib.js"

Modules.push( "Hitlist" );

var Hitlist = {
	count: 0,
	counter: 0,
	running: false,
	ignore: [],
	avoid: [],
	attacking: false,
	attacks: 1,
	log: function(action) {
		var log = [{
			name: 'Script',
			value: 'Hitlist'
		},
		{
			name: 'Action',
			value: action
		}];
		Console.log(log);
		Console.status('hitlist', action);
		return;
	},
	ignore_check: function() {
		for (var i = Hitlist.ignore.length - 1; i >= 0; i--) {
			if (Hitlist.user == Hitlist.ignore[i]) {
				return true;
			}
		}
		return false;
	},
	init: function(resume) {
		if (resume) {
			if (Hitlist.running === false) {
				return;
			};
		}
		Hitlist.count = 0;
		Hitlist.counter = 0;
		Hitlist.ignore = Boss.ignore.concat(Hitlist.avoid);
		Hitlist.running = true;
		Hitlist.attacking = false;
		Hitlist.attacks = 1;
		Hitlist.log('Initiating Hitlist Automation');
		Ajax(Page.domain + '/mobwars/hitlist/', function(xhr) {
			Boss.update(xhr.responseText);
			Hitlist.stats_check();
			return;
		});
		return;
	},
	stats_check: function() {
		if (Hitlist.running === false) {
			return;
		};
		if (Page.pausing === true) {
			Page.pause_cb(function() {
				Hitlist.stats_check();
				return;
			});
			return;
		};
		Hitlist.log('Checking Boss\'s Stats');
		var updating = false;
		if (Boss.cash >= Settings.bank_limit && Settings.bank && Cache.banking === false) {
			Hitlist.log('Banking ' + Utility.int_dollars(Boss.cash));
			Task.bank(Hitlist.stats_check);
			updating = true;
		}
		if (Settings.hitlist_stamina_stop > Boss.stamina) {
			Hitlist.log('Recharging Boss\'s Stamina');
			var timer = new Utility.timer({
				script: 'hitlist',
				message: 'Waiting [[time]] for Stamina to recharge...',
				time: stamina_wait * (Settings.hitlist_stamina_start - Boss.stamina),
				fn: function() {
					Ajax(Page.domain + '/mobwars/fight/', function(xhr) {
						Page.captcha(xhr.responseText);
						if (Hitlist.running === false) {
							return;
						};
						if (Page.pausing === true) {
							Page.pause_cb(function() {
								Hitlist.stats_check();
								return;
							});
							return;
						};
						Boss.update(xhr.responseText);
						Hitlist.stats_check();
						return;
					});
				}
			});
			timer.start();
			updating = true;
		} else if (Boss.health <= Settings.min_health && Settings.heal && Cache.healing === false) {
			Hitlist.log('Healing your Boss');
			Task.heal(Hitlist.stats_check);
			updating = true;
		}
		if (updating) {
			Hitlist.list = null;
			return;
		}
		Hitlist.log('Stat checks complete');
		Hitlist.log('Checking Hitlist');
		var timer = new Utility.timer({
			time: Page.wait(),
			message: 'Checking Hitlist in [[time]]...',
			script: 'hitlist',
			fn: function() {
				Hitlist.load_attack();
				return;
			}
		});
		timer.start();
		return;
	},
	load_attack: function() {
		Ajax(Page.domain + '/mobwars/hitlist/', function(xhr) {
			Page.captcha(xhr.responseText);
			if (Hitlist.running === false) {
				return;
			};
			if (Page.pausing === true) {
				Page.pause_cb(function() {
					Hitlist.stats_check();
				});
				return;
			};
			Boss.update(xhr.responseText);
			Hitlist.check(xhr.responseText);
			return;
		});
		return;
	},
	check: function(html) {
		var tre = 'name="target_id" value="(.*?)"',
			bre = 'name="bounty_id" value="(.*?)"',
			vre = 'name="verify" value="(.*?)"',
			cre = '<td>\\$([0-9]{1,4}(?:,[0-9]{3})*?)</td>';
		if (ms = html.match(new RegExp(tre, 'g'))) {
			var cashs = html.match(new RegExp(cre, 'g')),
				b_ids = html.match(new RegExp(bre, 'g')),
				vs = html.match(new RegExp(vre, 'g')),
				tre = new RegExp(tre),
				bre = new RegExp(bre),
				cre = new RegExp(cre),
				vre = new RegExp(vre);
			for (var i = (ms.length - 1); i >= 0; i--) {
				var cash = Utility.make_number(cashs[i].match(cre)[1]);
				if (Settings.min_bounty <= cash && (Settings.max_bounty >= cash || 0 == Settings.max_bounty)) {
					Hitlist.user = ms[i].match(tre)[1];
					if (Hitlist.ignore_check() === false) {
						var b_id = b_ids[i].match(bre)[1],
							verify = vs[i].match(vre)[1];
						Hitlist.log('User ' + Hitlist.user + ' found. Attacking');
						Hitlist.counter = 0;
						Hitlist.attack(b_id, verify);
						return;
					}
				}
			}
		}
		Hitlist.stats_check();
		return;
	},
	attack: function(b_id, verify) {
		Hitlist.attacks = Settings.hitlist_multi_attack;
		if (0 >= Settings.hitlist_multi_attack) {
			Hitlist.attacks = 1;
		}
		Hitlist.attacking = true;
		var count = Hitlist.count;
		for (var i = Hitlist.attacks - 1; i >= 0; --i) {
			Ajax(Page.domain + '/mobwars/fight/do.php?from=hitlist&action=attack_bounty&target_id=' + Hitlist.user + '&bounty_id=' + b_id +   '&verify=' + encodeURIComponent(verify), function(xhr) {
				Hitlist.counter++;
				var ann = Task.announce(xhr.responseText);
				if (ann) {
					if (ann.bounty) {
						Hitlist.log('Bounty Claimed from ' + Hitlist.user + '! Gained ' + Utility.int_dollars(ann.cash) + '.');
					}
					if (ann.won) {
						Hitlist.log('Hitlist Fight Won against ' + Hitlist.user + '! You dealt ' + ann.damaged + ' damage, recieved ' + ann.damager + ', gained   ' + ann.cash_str + ' and ' + ann.exp + ' exp');
					} else if (ann.lost) {
						if (!Histlist.ignore_check(Hitlist.user)) Hitlist.ignore.push(Hitlist.user);
						Hitlist.log('Hitlist Fight Lost against ' + Hitlist.user + '! You dealt ' + ann.damaged + ' damage, recieved ' + ann.damager);
					} else if (ann) {
						Hitlist.log('Hitlist Attack failed! "' + ann.message + '"');
					}
				}
				Page.captcha(xhr.responseText);
				if (Hitlist.count >= Settings.script_repeat && -1 != Settings.script_repeat) {
					Page.toggle('hitlist');
					return;
				}
				if (Hitlist.running === false) {
					return;
				};
				if (Page.pausing === true) {
					Page.pause_cb(function() {
						Hitlist.stats_check();
						return;
					});
					return;
				};
				Boss.update(xhr.responseText);
				if (Hitlist.attacks <= Hitlist.counter) {
					Hitlist.attacking = false;
					Hitlist.count++;
					Hitlist.stats_check();
				}
				return;
			}, "post");
		}
		var fn = function(count) {
			if (count <= Hitlist.count) return;
			if (Hitlist.attacking) {
				Hitlist.attacking = false;
				Hitlist.count++;
				Hitlist.stats_check();
			}
		};
		setTimeout(fn, 15000 * Hitlist.attacks, count);
	}
};
#endif
